package cc.rengu.igas.route.common.dao.impl;

import cc.rengu.igas.route.common.dao.DyncDataStaticMapper;
import cc.rengu.igas.route.common.entity.DyncDataStatic;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能路由动态数据统计表操作类
 *
 * @author xlz
 * @version 1.0
 * @date 2020-03-13
 */
public class DyncDataStaticMapperImpl implements DyncDataStaticMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.ROUTE_POOL_NAME);

    public DyncDataStaticMapperImpl() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.ROUTE_POOL_NAME);
        }
    }

    @Override
    public DyncDataStatic getDyncDataStatic(String instId, String transChannelId, String machineId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getDyncDataStatic");
        if (StringUtil.isNullorEmpty(sqlStr)) {
            throw new Exception("获取数据操作SQL失败");
        }

        DyncDataStatic dyncDataStatic = new DyncDataStatic();
        dyncDataStatic.setChannelId(transChannelId);
        dyncDataStatic.setInstId(instId);
        dyncDataStatic.setMachineId(machineId);
        int iRetCode = dbsUtil.dbsExecuteSql(sqlStr, dyncDataStatic, dyncDataStatic);
        if (Database.DBS_SUCCESS == iRetCode) {
            return dyncDataStatic;
        } else if (Database.DBS_NOT_FOUND == iRetCode) {
            return null;
        } else {
            throw new Exception("主键查询智能路由动态数据统计表失败");
        }
    }

    @Override
    public List<DyncDataStatic> getDyncDataStaticByMachine(String instId, String machineId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getDyncDataStaticByMachine");
        if (StringUtil.isNullorEmpty(sqlStr)) {
            throw new Exception("获取数据操作SQL失败");
        }

        DyncDataStatic dyncDataStatic = new DyncDataStatic();
        dyncDataStatic.setInstId(instId);
        dyncDataStatic.setMachineId(machineId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, dyncDataStatic, DyncDataStatic.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("指定机器查询智能路由动态统计数据为空!");
            return null;
        }
        return objectList.stream().map(item -> (DyncDataStatic) item).collect(Collectors.toList());

    }

    @Override
    public int insertDyncDataStatic(DyncDataStatic dyncDataStatic) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertDyncDataStatic");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dyncDataStatic, null);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_PKEY_DUPLICATE != iReturnCode) {
            rglog.error("登记智能路由动态统计数据表失败,instId:<{}>,channelId:<{}>,machineId:<{}>",
                    dyncDataStatic.getInstId(), dyncDataStatic.getChannelId(), dyncDataStatic.getMachineId(), iReturnCode);
            throw new Exception("插入智能路由动态统计数据错误");
        } else {
            return iReturnCode;
        }
    }

    @Override
    public void updDyncDataStatic(DyncDataStatic dyncDataStatic) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updDyncDataStatic");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dyncDataStatic, null);
        if (0 != iReturnCode) {
            rglog.error("更新智能路由动态统计数据表失败,instId:<{}>,channelId:<{}>,machineId:<{}>",
                    dyncDataStatic.getInstId(), dyncDataStatic.getChannelId(), dyncDataStatic.getMachineId(), iReturnCode);
            throw new Exception("更新智能路由动态统计数据错误");
        }
    }
}
